﻿using UnityEngine;
using System;
using System.Collections.Generic;
using DG.Tweening;

public class PlayerCharacter : Character
{
    //private GameObject m_gameObject;

    /*public enum CONTROL_STATE{
        NONE=0,
        CLIMBE=1
    };

    public enum STATE{
        NONE=0,
        CLIMBING=1,
        RUNNING=2,
        SLOWING=3
    };

    private CONTROL_STATE m_Control_State;
    private STATE m_State;
    private float m_ClimbeHigh;
    private float m_Climbex;
    private float m_Climbez;
    private float m_OriSpeed;

    //这些代码以后需要修改
    public CharacterController mCharact;
    float rotate;
    float translationx,translationz,translationy;
    float speed;
    float angle;
    float total;
    float movex,movez;
    Vector3 forward;
    Vector3 direction;
    Vector3 vel;
    public Transform m_camera;*/

    // Use this for initialization
    void Start()
    {
        //Init();
        /*m_Control_State = CONTROL_STATE.NONE;
        m_State = STATE.NONE;
        m_OriSpeed = 10.0f;
        speed = m_OriSpeed;
        vel = new Vector3 (1,1,1);*/

        //m_gameObject = this.gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        //Move();
        /*if (m_State == STATE.NONE || m_State == STATE.SLOWING || m_State == STATE.RUNNING) {
            total = m_camera.forward.x + m_camera.forward.z;

            //控制行走
            translationx = Input.GetAxis ("Horizontal") * speed;
            translationz = Input.GetAxis ("Vertical") * speed;

            //不让斜向移动比单向移动快
            if (Mathf.Abs(translationx) + Mathf.Abs(translationz) > speed) {
                translationx=translationx/(Mathf.Abs(translationx) + Mathf.Abs(translationz))*speed*1.4f;//根号2
                translationz=translationz/(Mathf.Abs(translationx) + Mathf.Abs(translationz))*speed*1.4f;
            }

            translationx *= Time.deltaTime;
            translationz *= Time.deltaTime;
            if (!mCharact.isGrounded) {
                translationy -= 5.0f * Time.deltaTime;
            }

            movex=translationx*m_camera.forward.z/total+translationz*m_camera.forward.x/total;
            movez=translationz*m_camera.forward.z/total-translationx*m_camera.forward.x/total;

            //控制转向
            if (Input.GetButton("Horizontal") || Input.GetButton("Vertical")) {

                forward = new Vector3 (movex/Mathf.Sqrt(movex*movex+movez*movez),0,movez/Mathf.Sqrt(movex*movex+movez*movez));
                angle=Vector3.Angle(transform.forward,forward);

                direction=Vector3.Cross(transform.forward,forward);
                if(direction.y>0){
                    //transform.DORotate(transform.eulerAngles+new Vector3(0,angle,0),0.5f);
                    //transform.eulerAngles=Vector3.Slerp(transform.eulerAngles,transform.eulerAngles+new Vector3(0,angle,0),1.0f);
                    transform.eulerAngles=Vector3.SmoothDamp(transform.eulerAngles,transform.eulerAngles+new Vector3(0,angle,0),ref vel,0.2f);
                }else{
                    //transform.DORotate(transform.eulerAngles-new Vector3(0,angle,0),0.5f);
                    //transform.eulerAngles=Vector3.Slerp(transform.eulerAngles,transform.eulerAngles-new Vector3(0,angle,0),1.0f);
                    transform.eulerAngles=Vector3.SmoothDamp(transform.eulerAngles,transform.eulerAngles-new Vector3(0,angle,0),ref vel,0.2f);
                }

            }
            mCharact.Move (new Vector3(movex,translationy,movez));
        }

        if (Input.GetButtonDown ("Run")) {
            m_State = STATE.RUNNING;
            speed *= 1.5f;
        }

        if (Input.GetButtonDown ("Slow")) {
            m_State = STATE.SLOWING;
            speed *= 0.5f;
        }

        if (Input.GetButtonUp ("Run")) {
            m_State = STATE.NONE;
            speed = m_OriSpeed;
        }

        if (Input.GetButtonUp ("Slow")) {
            m_State = STATE.NONE;
            speed = m_OriSpeed;
        }

        if (Input.GetButton ("Select")) {
            switch(m_Control_State){
            case CONTROL_STATE.CLIMBE:
                Climbe();
                break;
            default:
                break;
            }
        }*/
    }

    /*public void Jump(){
        if (mCharact.isGrounded && (m_State == STATE.NONE || m_State == STATE.RUNNING || m_State == STATE.SLOWING)) {
            translationy=1.0f;
        }
    }

    public void SetClimbeParam(float high, float x, float z){
        m_ClimbeHigh = high;
        m_Climbex = x;
        m_Climbez = z;
    }

    public void Climbe(){
        if (m_State != STATE.CLIMBING) {
            m_State = STATE.CLIMBING;
            transform.DOMove (new Vector3(transform.position.x+m_Climbex*transform.localScale.x/2,
                                          transform.position.y+m_ClimbeHigh,
                                          transform.position.z+m_Climbez*transform.localScale.z/2),1.0f).OnComplete(()=>{
                m_State = STATE.NONE;
                ControlState=CONTROL_STATE.NONE;
            });
        }
    }

    public CONTROL_STATE ControlState{
        get{ return m_Control_State;}
        set{ m_Control_State=value;}
    }

    public STATE State{
        get{return m_State;}
        set{m_State=value;}
    }

    public void OnTriggerEnter(Collider col){
        col.gameObject.GetComponent<TriggerBase> ().NoticeEnter(this,gameObject);
    }

    public void OnTriggerExit(Collider col){
        col.gameObject.GetComponent<TriggerBase> ().NoticeExit (this,gameObject);
    }*/
}